package com.blog.controller.admin;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blog.pojo.BlogType;
import com.blog.pojo.PageBean;
import com.blog.service.BlogTypeService;
import com.blog.util.UtilResponse;

/**
 * 管理员  -- 博客类别
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin/blogType")
public class BlogTypeAdminController {
	@Resource
	private BlogTypeService blogTypeService;
	
	/**
	 * 分页查询博客类别信息
	 * @param page
	 * @param rows
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(int page,int rows,HttpServletResponse response)throws Exception{
		//分页bean
		PageBean pageBean=new PageBean(page,rows);
		//分页条件
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		//查询出博客类型的结果列表
		List<BlogType> blogTypeList = blogTypeService.list(map);
		//查询总数量
		Long total=blogTypeService.getTotal();
		//JSON 集合序列化
		JSONObject result=new JSONObject();
		
		result.put("rows", blogTypeList);
		result.put("total", total);
		//调用 --返回数据
		UtilResponse.write(response, result);
		return null;
	}
	
	/**
	 * 保存博客类别信息
	 * @param blogType
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(BlogType blogType,HttpServletResponse response) throws Exception{
		int isok = 0;//操作状态
		//判断操作类型
		if(blogType.getId() == null){
			isok = blogTypeService.add(blogType);//添加
		}else{
			isok = blogTypeService.update(blogType);//修改
		}
		//JSON 返回集合
		JSONObject result = new JSONObject();
		//判断操作是否成功
		if(isok >0 ){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		//返回结果
		UtilResponse.write(response, result);
		return null;
	}
	
	/**
	 * 删除博客类型
	 * @param ids 多对象 xx,xx,xx......
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(String ids,HttpServletResponse response) throws Exception{
		//解析ids
		String idsStr[] = ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			//删除博客类型
			blogTypeService.delete(Integer.parseInt(idsStr[i]));
		}
		//返回成功
		JSONObject result=new JSONObject();
		result.put("success", true);
		UtilResponse.write(response, result);
		return null;
	}
	
}
